<?php


namespace app\common\service\lib;

use think\facade\Cache;

class Cos
{
    protected $client;

    public function __construct()
    {
        $secretId = config("setting.tencent.secretId"); //"云 API 密钥 SecretId";
        $secretKey = config("setting.tencent.secretKey"); //"云 API 密钥 SecretKey";
        $region = config("setting.tencent.region");; //设置一个默认的存储桶地域
        $cosClient = new \Qcloud\Cos\Client(
            array(
                'region' => $region,
                'schema' => 'https', //协议头部，默认为http
                'credentials' => array(
                    'secretId' => $secretId,
                    'secretKey' => $secretKey
                )
            )
        );
        $this->client = $cosClient;
    }

    public function bigCos($filename)
    {
        $redis = Cache::store('redis');
        $redis->select(9);
        $time = time();
        try {
            $result = $this->client->createMultipartUpload(array(
                'Bucket' => config("setting.tencent.bucket"), //格式：BucketName-APPID
                'Key' => $filename,
            ));
            $redis->set( $filename . 'upload', $result['UploadId'],600);
            $redis->set( $filename . 'key', $result['Key'],600);
            $redis->set($filename . 'bucket', $result['Bucket'],600);
        } catch (\Exception $e) {
            // 请求失败
            return ['code' => '400', 'data' => $e, 'message' => $e->getMessage()];
        }
    }

    public function bigCoss($path, $params, $filename)
    {
        $redis = Cache::store('redis');
        $redis->select(9);
        if ($params['page'] == 1) {
            self::bigCos($filename);
        }
        $uploadId = $redis->get($filename . 'upload');
        $key = $redis->get($filename . 'key');
        $bucket = $redis->get($filename . 'bucket');
        try {
            $result = $this->client->uploadPart(array(
                'Bucket' => $bucket, //格式：BucketName-APPID
                'Key' => $key,
                'Body' => $path,
                'UploadId' => $uploadId, //UploadId 为对象分块上传的 ID，在分块上传初始化的返回参数里获得
                'PartNumber' => $params['page'], //PartNumber 为分块的序列号，COS 会根据携带序列号合并分块
            ));
            return ['ETag' => $result['ETag'], 'PartNumber' => $params['page']];
        } catch (\Exception $e) {
            // 请求失败
            return ['code' => '400', 'data' => $e, 'message' => $e->getMessage()];
        }
    }

    public function complete($arr,$filename)
    {
        $redis = Cache::store('redis');
        $redis->select(9);
        $uploadId = $redis->get($filename . 'upload');
        $key = $redis->get($filename . 'key');
        $bucket = $redis->get($filename . 'bucket');
        try {
            $this->client->completeMultipartUpload(array(
                'Bucket' => $bucket,
                'Key' => $key,
                'UploadId' => $uploadId,
                'Parts' => $arr
            ));
        } catch (\Exception $e) {
            // 请求失败
            return ['code' => '400', 'data' => $e, 'message' => $e->getMessage()];
        }
    }
}